<template>
  <div class="page-container">
    <!-- 查询区域 -->
    <div class="search-bar">
      <label for="grade" class="form-label">年级：</label>
      <el-select
        v-model="selectedGrade"
        id="grade"
        placeholder="请选择年级"
        style="width: 180px; margin-right: 10px"
      >
        <el-option
          v-for="grade in gradeOptions"
          :key="grade.value"
          :label="grade.label"
          :value="grade.value"
        />
      </el-select>
      <el-button type="primary" @click="handleQuery">查询</el-button>
      <el-button type="success" @click="handleAdd">添加</el-button>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="id" label="Id" align="center" />
      <el-table-column prop="subject" label="学科" align="center" />
      <el-table-column prop="grade" label="年级" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleEdit(scope.row)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 20px"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElSelect, ElOption, ElButton, ElTable, ElTableColumn, ElPagination } from 'element-plus';

// 年级下拉选项
const gradeOptions = ref([
  { label: '一年级', value: '一年级' },
  { label: '二年级', value: '二年级' },
  { label: '三年级', value: '三年级' },
  { label: '六年级', value: '六年级' },
  { label: '初三', value: '初三' },
  { label: '高三', value: '高三' },
  { label: '高三年级', value: '高三年级' }
]);
// 选中的年级
const selectedGrade = ref('');
// 表格数据（模拟，实际需接口获取）
const tableData = ref([
  { id: 42, subject: '12313', grade: '二年级' },
  { id: 39, subject: '化学', grade: '高三年级' },
  { id: 37, subject: '物理', grade: '高三年级' },
  { id: 36, subject: '消防设施', grade: '一年级' },
  { id: 35, subject: '消防设施', grade: '一年级' },
  { id: 34, subject: '消防设施', grade: '一年级' },
  { id: 33, subject: 'yuwen', grade: '六年级' },
  { id: 32, subject: '语文', grade: '二年级' },
  { id: 31, subject: '语文', grade: '初三' },
  { id: 30, subject: '语文', grade: '高三' }
]);
// 分页相关
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(30); // 模拟总数，实际从接口取

// 查询按钮事件
const handleQuery = () => {
  console.log('查询条件：', selectedGrade.value);
  // 实际逻辑：调用接口，根据 selectedGrade 筛选数据
  // 示例：axios.get('/api/data', { params: { grade: selectedGrade.value, page: currentPage.value, pageSize: pageSize.value } })
  // .then(res => {
  //   tableData.value = res.data.list;
  //   total.value = res.data.total;
  // });
};

// 添加按钮事件
const handleAdd = () => {
  console.log('点击添加，可打开弹窗或跳转到添加页面');
  // 实际逻辑：打开添加弹窗或路由跳转
};

// 编辑按钮事件
const handleEdit = (row) => {
  console.log('编辑行数据：', row);
  // 实际逻辑：打开编辑弹窗，回显 row 数据
};

// 删除按钮事件
const handleDelete = (row) => {
  console.log('删除行数据：', row);
  // 实际逻辑：调用删除接口，移除 tableData 对应项
  // 示例：tableData.value = tableData.value.filter(item => item.id !== row.id);
  // total.value--;
};

// 每页条数改变
const handleSizeChange = (val) => {
  pageSize.value = val;
  handleQuery(); // 重新查询
};

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val;
  handleQuery(); // 重新查询
};
</script>

<style scoped>
.page-container {
  padding: 20px;
}
.search-bar {
  display: flex;
  align-items: center;
}
.form-label {
  margin-right: 8px;
}
</style>